<?php
/**
 * 缓存服务 单例模式
 * @author hipop@126.com
 *
 */
class Application_Service_Cache
{
	public  $_cacheObject;
	static  $_instance;
	private $_lifeTime;
	private $_userId;

	private function __construct() {
		$sysConf = new Zend_Session_Namespace(Bootstrap::APP_CONF_SESSION_KEY);
		$userInf = new Zend_Session_Namespace(Application_Model_Login::LOGIN_SESSION_KEY);
		
		$this->_lifeTime = ini_get("session.gc_maxlifetime");
		$frontend = array(
			//'lifeTime'=>600,//缓存时间与登录时间一致
			'lifeTime'=>$this->_lifeTime,
			'cache_id_prefix'=>$sysConf->cachePrefix."_",
			'automatic_serialization'=>TRUE
		);
		$backend = array(
			'cache_dir'=>$sysConf->cachePath
		);
		//$this->_cacheObject = Zend_Cache::factory('Core','Apc',$frontend, $backend);
		$this->_cacheObject = Zend_Cache::factory('Core',$sysConf->cacheMode,$frontend, $backend);
		
		$this->_userId = $userInf->_user_id;
	}

	private function __clone() {
		;
	}

	/**
	 * 修改缓存时间
     * @param  int $newLifetime New lifetime (in seconds)
     * @return void
	 */
	public function setLifteTime($newLifetime){
		$this->_lifeTime = $newLifetime;
		$this->_cacheObject->setLifetime($newLifetime);
	}
	
	/**
	 * 个人缓存
	 * @param any_type $data 数据
	 * @param any_type $id 缓存唯一标识
	 * @param any_type $tags 缓存标签
	 * @param boolean $specificLifetime 缓存生存时间
	 * @param int $priority  缓存优先级 0-10
	 * @throws Zend_Cache_Exception
     * @return boolean True if no problem
	 */
	public function save($data, $id = null, $tags = array(), $specificLifetime = false, $priority = 8) {
		$id = $this->_userId.'_'.$id;
		return $this->_cacheShop->save($data,$id,$tags,$specificLifetime,$priority);
	}

	/**
	 * 读取个人级别的缓存
	 * @param any_type $id
     * @param  boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
     * @param  boolean $doNotUnserialize       Do not serialize (even if automatic_serialization is true) => for internal use
     * @return mixed|false Cached datas
	 */ 
	public function load($id, $doNotTestCacheValidity = false, $doNotUnserialize = false) {
		$id = $this->_userId.'_'.$id;
		return $this->_cacheShop->load($id, $doNotTestCacheValidity = false, $doNotUnserialize = false);
	}
	
	/**
	 * 个人的缓存数据删除
	 * @param any_type $id
	 */
	public function remove($id) {
		$id = $this->_userId.'_'.$id;
		return $this->_cacheShop->remove($id);
	}

	/**
     * Give (if possible) an extra lifetime to the given cache id
     *
     * @param string $id cache id
     * @param int $extraLifetime
     * @return boolean true if ok
     */
    public function touch($id, $extraLifetime){
    	$id = $this->_userId.'_'.$id;
    	return $this->_cacheObject->touch($id, $extraLifetime);
    }
    
	/**
	 * 全局的缓存
	 * @param any_type $data 数据
	 * @param any_type $id 缓存唯一标识
	 * @param any_type $tags 缓存标签
	 * @param boolean $specificLifetime 缓存生存时间
	 * @param int $priority  缓存优先级 0-10
	 * 
	 * @throws Zend_Cache_Exception
     * @return boolean True if no problem
	 */
	public function saveG($data, $id = null, $tags = array(), $specificLifetime = false, $priority = 8) {
		$specificLifetime = 0;//永久缓存
		$id = 'Global_'.$id;		
		return $this->_cacheObject->save($data,$id,$tags,$specificLifetime,$priority);
	}
		
	/**
	 * 全局的数据缓存加载
	 * @param any_type $id
     * @param  boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
     * @param  boolean $doNotUnserialize       Do not serialize (even if automatic_serialization is true) => for internal use
     * @return mixed|false Cached datas
	 */
	public function loadG($id, $doNotTestCacheValidity = false, $doNotUnserialize = false){
		$id = 'Global_'.$id;
		return $this->_cacheObject->load($id, $doNotTestCacheValidity, $doNotUnserialize);
	}
	
	/**
	 * 全局的缓存数据删除
	 * @param any_type $id
	 */
	public function removeG($id){
		$id = 'Global_'.$id;
		return $this->_cacheObject->remove($id);
	}
	
	/**
	 * 批量清除缓存
	 * @param string $mode
	 * @param string $tags
	 */
	function clean($mode = 'all', $tags = array()) {
		return $this->_cacheObject->clean($mode, $tags);
	}

	/**
     * Give (if possible) an extra lifetime to the given cache id
     *
     * @param string $id cache id
     * @param int $extraLifetime
     * @return boolean true if ok
     */
    public function touchG($id, $extraLifetime){
    	return $this->_cacheObject->touch($id, $extraLifetime);
    }
	/**
	 * 获取缓存器的单例
	 */
	public static function getInstance() {
		if(!(self::$_instance instanceof self)){
			self::$_instance = new self();
		}
		return self::$_instance;
	}

}
